<?php

/**
 *    文章管理控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
class PlquanlyApp extends BackendApp
{
    var $_dangtin_mod;
    function __construct()
    {
        $this->PlquanlyApp();
    }
    function PlquanlyApp()
    {
        parent::BackendApp();
        $this->_dangtin_mod = &m('pldangtin');
        
    }
    function index()
    {  
    	//$user=$this->visitor->get('user_id');
    	$page = $this->_get_page(5);
         $conditions = $this->_get_query_conditions(array(array(
                'field' => 'title',
                'equal' => 'LIKE',
                'limit' => $page['limit'],
                'assoc' => 'AND',
                'name'  => 'title',
                'type'  => 'string',
               
            ),
        ));
         
         //var_dump($user);
         if (isset($_GET['sort']) && isset($_GET['order']))
        {
            $sort  = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order,array('asc','desc')))
            {
             $sort  = 'id';
             $order = 'desc';
            }
        }
        else
        {
            $sort  = 'id';
            $order = 'desc';
        }
    	$model_dangtin =& m('pldangtin');
        $dangtin = $this->_dangtin_mod->find(array(
            'limit'         => $page['limit'],
	        'order'         => "$sort $order",
	        'count'         => true,
	        //'conditions'    => 'user_id = ' . $this->visitor->get('user_id'),
        ));

        $this->assign('dangtin', $dangtin);
        $this->assign('scategories', $this->_get_scategory_options());
    	 $this->_get_regions();
    	 
        $page['item_count'] = $this->_dangtin_mod->getCount();
         $this->import_resource(array('script' => 'inline_edit.js'));
        $this->_format_page($page);
        $this->assign('filtered', $filter? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);
        
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->assign('wait_verify', $_GET['wait_verify']);
        $this->assign('page_info', $page); 
        $this->display('pldangtin.index.html');
    }
     function _get_regions()
    {
        $model_region =& m('region');
        $regions = $model_region->get_list(0);
        if ($regions)
        {
            $tmp  = array();
            foreach ($regions as $key => $value)
            {
                $tmp[$key] = $value['region_name'];
            }
            $regions = $tmp;
        }
        $this->assign('regions', $regions);
    }
     function _get_scategory_options()
    {
        $mod =& m('scategory');
        $scategories = $mod->get_list();
        import('tree.lib');
        $tree = new Tree();
        $tree->setTree($scategories, 'cate_id', 'parent_id', 'cate_name');
        return $tree->getOptions();
    }
      function drop()
    {
    	 $user_id = isset($_GET['user_id']) ? trim($_GET['user_id']) : '';
    	  $id = isset($_GET['id']) ? trim($_GET['id']) : '';
    	
       
        //var_dump($id);
         if (!$id)
        {
            $this->show_warning('no_such_brand');

            return;
        }
        $id=explode(',', $id);
        $this->_dangtin_mod->drop($id);
        if ($this->_dangtin_mod->has_error())    //删除
        {
            $this->show_warning($this->_dangtin_mod->get_error());

            return;
        }
                  
        $xoa_anh=isset($_GET['picture']) ? trim($_GET['picture']) : '';
        unlink('../'.$xoa_anh);
                           
        $this->show_message('drop_successed');
    }
    function edit()
    {
    	 if (!IS_POST)
        {
            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js'
            ));
            $this->assign('build_editor', $this->_build_editor(array(
                'name' => 'description',
                'content_css' => SITE_URL . "/themes/store/{$template_name}/styles/{$style_name}" . '/shop.css', // for preview
            )));
             $this->_get_regions();
             $this->assign('scategories', $this->_get_scategory_options());

	        $id = isset($_GET['id']) ? trim($_GET['id']) : '';
            $find_data  = $this->_dangtin_mod->find($id);
            if (empty($find_data))
            {
                $this->show_warning('no_such_brand');

                return;
            }
            $dtin    =   current($find_data);
            //var_dump($dtin);
            $this->assign('dtin', $dtin);
            $region_mod =& m('region');
            $this->assign('regions', $region_mod->get_options(0));
           
            $this->import_resource('jquery.plugins/jquery.validate.js,mlselection.js');
            $this->display('edit_new.form.html');
        }
        else
        {
        	 
             $id = isset($_GET['id']) ? trim($_GET['id']) : '';
             $user_id = isset($_GET['user_id']) ? trim($_GET['user_id']) : '';
             $anh=$_FILES['picture']['name'];
             $ngaydang=time();
            
             if($anh !='')
             {               
                               	 $xoa_anh=isset($_GET['picture']) ? trim($_GET['picture']) : '';
             	                 unlink('../'.$xoa_anh);
                              
             	                //xoa file anh het
								$file_name=$_FILES['picture']['name'];
								$file_path=$_FILES['picture']['tmp_name'];
								if( $user_id ==0)
								{
							       move_uploaded_file($_FILES["picture"]["tmp_name"],"../data/files/mall/anh_0/" . $_FILES["picture"]["name"]);
							       $new_path="../data/files/mall/anh_0/".$file_name;
								}
								else {
									$abcd="../data/files/mall/anh_".$user_id;
									$bbc="../data/files/mall/anh_".$user_id."/";
									if (!file_exists( $abcd )) {
										mkdir("../data/files/mall/anh_".$user_id);
										$dir="../data/files/mall/anh_".$user_id."/";
										//var_dump($dir);
										move_uploaded_file($_FILES["picture"]["tmp_name"],$dir . $_FILES["picture"]["name"]);
									    $new_path=$dir.$file_name;
									}
									 move_uploaded_file($_FILES["picture"]["tmp_name"],$bbc . $_FILES["picture"]["name"]);
									 $new_path=$bbc.$file_name;
					
								}
			                $cut_str=substr($new_path,3);
				           $data = array(
				                       // 'user_id'     => $user_id,
				                        'picture'   => $cut_str,
				                        'title'   => $_POST['title'],
				                        'categories'   => $_POST['categories'],
				                        'address'    => $_POST['region_name'],
				                        'content'      => $_POST['description'],
				                        'thoigianup' => $ngaydang,
				                        'region_id'  => $_POST['region_id'],
				                    );
				            $this->_dangtin_mod->edit($id, $data);
				           
				           
				            if ($this->_dangtin_mod->has_error())
				            {
				                $this->pop_warning($this->_dangtin_mod->get_error());
				                return;
				            }
				            
				            $this->show_message('edit_ok',
				                'back_list',    'index.php?app=plquanly'
				            );
            }
            else
            {
            	//echo 'q111111111';
            	$data = array(
			                        //'user_id'     => $user_id,
			                       // 'picture'   => $new_path,
			                        'title'   => $_POST['title'],
			                        'categories'   => $_POST['categories'],
			                        'address'    => $_POST['region_name'],
			                        'content'      => $_POST['description'],
			                        'thoigianup' => $ngaydang,
			                         'region_id'  => $_POST['region_id'],
			                    );
			            $this->_dangtin_mod->edit($id, $data);
			            if ($this->_dangtin_mod->has_error())
			            {
			                $this->pop_warning($this->_dangtin_mod->get_error());
			                return;
			            }
			            
			            $this->show_message('edit_ok',
			                'back_list',    'index.php?app=plquanly'
			            );
			        
            }
         }
    	
    }
}

?>